
//------------------------------------------------------------------------------

using Apps.Models;
using Apps.IDAL.CY;
using System;
using System.Linq;

namespace Apps.DAL.CY
{
    public partial class CY_ADJUSTRepository : BaseRepository<CY_ADJUST>, ICY_ADJUSTRepository, IDisposable
    {
        public CY_ADJUSTRepository(DBContainer db) : base(db)
        {

        }

        public CY_ADJUSTModel QueryADJUST(string ADJUST_ID)
        {
            IQueryable<CY_ADJUSTModel> result = from a in Context.CY_ADJUST
                                                join sf in Context.PUB_CY_STOCKPILE on a.STOCKPILE_FROM equals sf.STOCKPILE_ID into sf
                                                from tsf in sf.DefaultIfEmpty()
                                                join st in Context.PUB_CY_STOCKPILE on a.STOCKPILE_TO equals st.STOCKPILE_ID into st
                                                from tst in sf.DefaultIfEmpty()
                                                where a.ADJUST_ID == ADJUST_ID
                                                select new CY_ADJUSTModel()
                                                {
                                                    ADJUST_ID = a.ADJUST_ID,
                                                    ADJUST_MARK = a.ADJUST_MARK,
                                                    ADJUST_TIME = a.ADJUST_TIME,
                                                    ADJUST_USER = a.ADJUST_USER,
                                                    STOCKPILE_FROM = a.STOCKPILE_FROM,
                                                    STOCKPILE_TO = a.STOCKPILE_TO,
                                                    PARTITION_FROM_NAME = tsf.PUB_CY_PARTITION.PARTITION_NAME,
                                                    PARTITION_TO_NAME = tst.PUB_CY_PARTITION.PARTITION_NAME,
                                                    STOCKPILE_FROM_NAME = tsf.STOCKPILE_NAME,
                                                    STOCKPILE_TO_NAME = tst.STOCKPILE_NAME
                                                };
            return result.FirstOrDefault();
        }

        public IQueryable<CY_ADJUSTModel> QueryADJUSTList(string QueryDate)
        {
            DateTime dtStartQueryDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime dtEndQueryDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

            int empty = 0;
            if (!string.IsNullOrWhiteSpace(QueryDate))
            {
                dtStartQueryDate = Convert.ToDateTime(QueryDate.Trim() + " 00:00:00");
                dtEndQueryDate = Convert.ToDateTime(QueryDate.Trim() + " 23:59:59");
            }
            else
            {
                empty = 1;
            }


            IQueryable<CY_ADJUSTModel> result = from a in Context.CY_ADJUST
                                                join sf in Context.PUB_CY_STOCKPILE on a.STOCKPILE_FROM equals sf.STOCKPILE_ID into sf
                                                from tsf in sf.DefaultIfEmpty()
                                                join st in Context.PUB_CY_STOCKPILE on a.STOCKPILE_TO equals st.STOCKPILE_ID into st
                                                from tst in sf.DefaultIfEmpty()
                                                join su in Context.SysUser on a.ADJUST_USER equals su.UserName into ssu
                                                from tsu in ssu.DefaultIfEmpty()
                                                where (a.ADJUST_TIME >= dtStartQueryDate && a.ADJUST_TIME <= dtEndQueryDate) || empty==1
                                                select new CY_ADJUSTModel()
                                                {
                                                    ADJUST_ID = a.ADJUST_ID,
                                                    ADJUST_MARK = a.ADJUST_MARK,
                                                    ADJUST_TIME = a.ADJUST_TIME,
                                                    ADJUST_USER = a.ADJUST_USER,
                                                    STOCKPILE_FROM = a.STOCKPILE_FROM,
                                                    STOCKPILE_TO = a.STOCKPILE_TO,
                                                    PARTITION_FROM_NAME = tsf.PUB_CY_PARTITION.PARTITION_NAME,
                                                    PARTITION_TO_NAME = tst.PUB_CY_PARTITION.PARTITION_NAME,
                                                    STOCKPILE_FROM_NAME = tsf.STOCKPILE_NAME,
                                                    STOCKPILE_TO_NAME = tst.STOCKPILE_NAME,
                                                    ADJUST_USERName = tsu.TrueName
                                                };
            return result;
        }
    }
}
